<template>
  <div class="tenantMenu_main main_container">
    <div class="main_header">
      <div class="padding_container">
        <div class="left"><Breadcrumb /></div>
        <div class="right">
          <HeaderOperation :parent="this" />
        </div>
      </div>
    </div>
    <div v-loading="loading" class="main_main">
      <SearchBar :parent="this" />
      <div ref="table_container" class="table_container">
        <el-table
          ref="table"
          :data="tableData"
          stripe
          border
          :height="tableHeight"

          :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
          :cell-style="cellStyle"
          @cell-click="cellClick"
          @sort-change="(column)=>{sortChange(column, this)}"
          @selection-change="(val)=>{selectionChange(val, this)}"
        >
          <el-table-column type="selection" align="center" />
          <Column v-for="(column,index) in tableColumns" :key="index" :prop="column.prop" :is-show="column.isShow" :label="column.label" :sortable="column.sortable" :width="column.width" />
          <el-table-column label="操作" align="center" fixed="right" width="120">
            <template slot-scope="scope">
              <el-button
                v-if="scope.row.status =='0'"
                v-hasPermi="['wms:otherIn:edit']"
                type="text"
                @click.stop="edit(scope.row)"
              >编辑</el-button>
              <span v-else class="el-button--text button_grey">编辑</span>
              <el-button
                v-if="scope.row.status =='0'"
                v-hasPermi="['wms:otherIn:delete']"
                type="text"
                @click.stop="deleteOne(scope.row)"
              >删除</el-button>
              <span v-else class="el-button--text button_grey">删除</span>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <div class="main_bottom">
      <pagination
        :total="total"
        :page.sync="pageData.pageNum"
        :limit.sync="pageData.pageSize"
        :selected-num="selectionsAll.length"
        @pagination="getList"
      />
    </div>

  </div>
</template>

<script>
import { list, remove, getInfo } from '@/api/warehouseManagement/elseEnterWarehouse'
import { getDictSelects, getPageSettings } from '@/api/common/common'
import AddOrUpdate from './addOrUpdate'
import ShowDetail from './ShowDetailTitle' // 查看页面
export default {
  name: 'Index',
  data() {
    return {
      loading: true,
      pageId: '其他入库单', // 页面唯一标识
      pageData: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: 'formCode',
        isAsc: 'desc'
      },
      // tableKey: 1,
      total: 1, // 数据一共有多少条
      mainId: 'id', // 表格行ID
      tableHeight: '0', // 表格高度
      tableData: [], // 表格数据
      tableColumns: [], // 表格实际用的字段
      searchColumns: [], // 搜索区实际用的字段
      selections: [], // 表格当前页所选中的行
      selectionsAll: [], // 表格所有页选中的行
      defaultTableColumns: [// 表格常规字段（初始值）
        { prop: 'statusName', label: '状态' },
        { prop: 'formCode', label: '其他入库单号' },
        { prop: 'inByName', label: '入库人员', canOrder: false },
        { prop: 'inTime', label: '入库时间' },
        { prop: 'formTypeName', label: '入库类型', canOrder: false },
        { prop: 'bindFormCode', label: '关联单据' },
        { prop: 'remark', label: '备注', canOrder: false }

      ],
      defaultSearchColumns: [// 搜索区常规字段（初始值）
        { prop: 'formCode', label: '其他入库单号', type: 'input' },
        { prop: 'inByName', label: '入库人员', type: 'input' },
        { prop: 'formTypeId', label: '入库类型', type: 'select', optionsName: 'formType' },
        { prop: 'status', label: '状态', type: 'select', optionsName: 'statusOptions' },
        { prop: 'inStockTime', label: '入库时间', type: 'time' },
        { prop: 'bindFormCode', label: '关联单据', type: 'input' }
      ],
      operationData: [ // 右上角的操作按钮
        { label: '新增', perm: 'wms:otherIn:add' },
        // { label: '打印', perm: '' },
        // { label: '批量导出', perm: 'wms:otherIn:export' },
        { label: '批量删除', perm: 'wms:otherIn:delete' },
        { label: '页面配置', perm: '' }
      ],
      statusOptions: [ // 选择组件数据源
        { value: undefined, label: '全部' },
        { value: '1', label: '已入库' },
        { value: '0', label: '待入库' }
      ],
      formType: [],
      showKey: {
        type: 'column_two',
        keys: [
          { type: 'form', name: '其他入库单号', key: 'formCode' }, // type: 'form' 表示是普通的表单展示
          { type: 'form', name: '入库人员', key: 'inByName' },
          { type: 'form', name: '入库时间', key: 'inTime' },
          { type: 'form', name: '入库类型', key: 'formTypeName' },
          { type: 'form', name: '关联单据', key: 'bindFormCode' }, // length:'double' 表示占两列
          { type: 'file', name: '附件', fileName: 'fileName', fileRealName: 'fileRealName', fileUrl: 'fileUrl' },
          { type: 'form', name: '备注', key: 'remark', length: 'double' },
          {
            type: 'table', name: '入库明细', key: 'details', columns: [ // type: 'table' 表示是表格
              { type: 'column', name: '物料编码', key: 'materialCode', minWidth: '120' }, // type: 'form' 表示是普通的表单展示
              { type: 'column', name: '物料名称', key: 'materialName', minWidth: '100' },
              { type: 'column', name: '规格型号', key: 'materialModel', minWidth: '100' },
              { type: 'column', name: '物料分类', key: 'materialTypeName', minWidth: '100' },
              { type: 'column', name: '物料单位', key: 'materialUnitName', minWidth: '100' },
              { type: 'column', name: '入库数量', fixed: 'right', key: 'inStockNum', minWidth: this.SETTINGS.settings_decimalDigits_length },
              { type: 'column', name: '批号', key: 'palletBatchCode', minWidth: '150' }, // type: 'form' 表示是普通的表单展示
              { type: 'column', name: '入库仓库', fixed: 'right', key: 'warehouseName', minWidth: '100' },
              { type: 'column', name: '入库库位', fixed: 'right', key: 'locationName', minWidth: '100' },
              { type: 'column', name: '备注', key: 'remark', minWidth: '200' }
            ]
          }
        ]
      }
    }
  },
  created() {
    this.getDictOptions()
  },
  mounted() {
  },
  methods: {
    cellStyle({ row, column }) {
      if (column.property == 'formCode') {
        return {
          cursor: 'pointer',
          color: '#387bfc'
        }
      }
    },
    cellClick(row, column, cell) {
      switch (column.property) {
        case 'formCode':
          this.showDetail(row)
          break
      }
    },
    getDictOptions() {
      getDictSelects('enterWarehouseType').then(res => {
        res.data.forEach(item => {
          item['label'] = item.dictLabel
          item['value'] = item.dictCode
        })
        this.formType = res.data
      })
    },
    getExportData(data) {
      return list(data)
    },
    refresh() {
      this.pageData.pageNum = 1
      this.getList()
    },
    refresh2() {
      this.tableData = []
      setTimeout(() => {
        this.getList()
      }, 50)
    },
    getList() {
      list(this.pageData).then(res => {
        this.tableData = res.data
        this.total = res.total
        this.loading = false
        this.$nextTick(() => {
          this.setSelectRow(this)// 设置行选中
          this.tableHeight = this.$refs.table_container.offsetHeight
        })
      })
    },
    showDetail(row) {
      getInfo(row[this.mainId]).then(res => {
        getPageSettings({ userId: this.userId, pageId: '其他入库单新增或编辑' }).then(res2 => {
          let cacheColumns = []
          if (res2.data.json) {
            const pageSettings = res2.data.json

            const tableColumnsCache = JSON.parse(JSON.stringify(pageSettings.TableColumns)) // 解决传值过来无法修改值的bug
            tableColumnsCache.filter(item => item.isShow).forEach(iitem => {
              iitem.type = 'column'
              iitem.name = iitem.label
              iitem.key = iitem.prop
              cacheColumns.push(iitem)
            })
            cacheColumns.push(...[
              { type: 'column', name: '入库数量', fixed: 'right', key: 'inStockNum', Width: this.SETTINGS.settings_decimalDigits_length },
              { type: 'column', name: '入库仓库', fixed: 'right', key: 'warehouseName', minWidth: '100' },
              { type: 'column', name: '入库库位', fixed: 'right', key: 'locationName', minWidth: '100' }

            ])
          } else {
            cacheColumns = [ // type: 'table' 表示是表格
              { type: 'column', name: '物料编码', key: 'materialCode', minWidth: '120' }, // type: 'form' 表示是普通的表单展示
              { type: 'column', name: '物料名称', key: 'materialName', minWidth: '100' },
              { type: 'column', name: '规格型号', key: 'materialModel', minWidth: '100' },
              { type: 'column', name: '物料分类', key: 'materialTypeName', minWidth: '100' },
              { type: 'column', name: '物料单位', key: 'materialUnitName', minWidth: '100' },
              { type: 'column', name: '批号', key: 'palletBatchCode', minWidth: '150' }, // type: 'form' 表示是普通的表单展示
              { type: 'column', name: '生产日期', key: 'productDate', minWidth: '150' }, // type: 'form' 表示是普通的表单展示
              { type: 'column', name: '备注', key: 'remark', minWidth: '160' },
              { type: 'column', name: '入库数量', key: 'inStockNum', Width: this.SETTINGS.settings_decimalDigits_length },
              { type: 'column', name: '入库仓库', fixed: 'right', key: 'warehouseName', minWidth: '100' },
              { type: 'column', name: '入库库位', fixed: 'right', key: 'locationName', minWidth: '100' }

            ]
          }
          this.showKey.keys[7].columns = cacheColumns
          console.log(this.showKey)
          this.$layer.iframe({// 弹框
            content: {
              content: ShowDetail, // 传递的组件对象
              parent: this, // 当前的vue对象
              data: {
                showData: res.data,
                showKey: this.showKey
              }// props
            },
            area: ['1320px', '700px'],
            title: '查看其他入库单', // 标题
            shadeClose: false // 点击遮罩是否关闭
          })
        })
      })
    },
    add() {
      this.$layer.iframe({// 弹框
        content: {
          content: AddOrUpdate, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {}// props
        },
        area: ['1320px', '700px'],
        title: '新增其他入库单', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    },
    edit(row) {
      this.$layer.iframe({// 弹框
        content: {
          content: AddOrUpdate, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
            id: row[this.mainId]
          }// props
        },
        area: ['1320px', '700px'],
        title: '编辑其他入库单', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    },
    deleteOne(row) {
      const _this = this
      this.$confirm('确定要删除吗', '删除确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消', cancelButtonClass: 'cancelConfirm',
        type: 'warning'
      }).then(function() {
        remove(row[_this.mainId]).then(res => {
          _this.$message({
            message: res.msg,
            type: 'success'
          })
          _this.selectionsAll = []
          _this.getList()
        })
      }).catch(() => {})
    },
    // 当前页的删除接口
    deleteMethod(deleteIds) {
      return remove(deleteIds)
    }
  }
}
</script>

<style lang="less" scoped>
</style>
